<template>
  <div id="cate">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        :data="cateList"
        :expand-type="false "
        show-index
        :selection-type="false"
        index-text="#"
        border
        :columns="columns"
      >
        <template slot="isok" slot-scope="scope">
          <i style="color:skyblue" v-if="scope.row.cat_deleted==false" class="el-icon-success"></i>
          <i style="color:red" v-else class="el-icon-error"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" type="primary" v-if="scope.row.cat_level==0">一级</el-tag>
          <el-tag size="mini" type="success" v-if="scope.row.cat_level==1">二级</el-tag>
          <el-tag size="mini" type="warning" v-if="scope.row.cat_level==2">三级</el-tag>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 6, 7, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <el-dialog title="添加商品" :visible.sync="AddCatedialogVisible" width="50%">
        <el-form :model="AddCateFrom" :rules="rules" ref="addCateRef" label-width="100px">
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="AddCateFrom.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              expand-trigger="hover"
              v-model="seletKeys"
              :options="parentCataList"
              :props="expandTrigger"
              @change="handleChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="AddCatedialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      parentCataList: [],
      seletKeys: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        }
      ],
      expandTrigger: {
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      AddCatedialogVisible: false,
      AddCateFrom: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      rules: {
        name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(newval) {
      this.queryInfo.pagesize = newval;
      this.getCateList();
    },
    handleCurrentChange(newval) {
      this.queryInfo.pagenum = newval;
      this.getCateList();
    },
    showAddCate() {
      this.AddCatedialogVisible = true;
      this.getParentCateList();
    },
    async getParentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      this.parentCataList = res.data;
      console.log(this.parentCataList);
    },
    handleChange(key) {
      if (this.seletKeys.length > 0) {
        this.AddCateFrom.cat_pid = this.seletKeys[this.seletKeys.length - 1];
        this.AddCateFrom.cat_level = this.seletKeys.length;
        return;
      } else {
        this.AddCateFrom.cat_pid = 0;
        this.AddCateFrom.cat_level = 0;
      }
    },
    async addCate() {
      const { data: res } = await this.$http.post(
        "categories",
        this.AddCateFrom
      );
      this.$message.success("添加成功");
      this.getCateList();
      this.AddCatedialogVisible = false;
    }
  }
};
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 20px;
}
.el-cascader {
  width: 100%;
}
</style>